import { Controller, Post, Body, UseGuards, Get } from "@nestjs/common";
import { AuthGuard } from "@nestjs/passport";

import { UserRegisterService } from "../service/user/user.register.service";
import { AuthService } from "../service/auth/auth.service";
import { UserExistService } from "../service/user/user.exist.service";
import { NewsService } from "../service/news/news.service";

@Controller("/user")
export class UserController {
  constructor(
    private userExistService: UserExistService,
    private userRegisterService: UserRegisterService,
    private authService: AuthService
  ) {
  }

  @Post("exist")
  findOne(@Body() body: any) {
    return this.userExistService.userExist(body);
  }

  @Post("register")
  async register(@Body() body: any) {
    return await this.userRegisterService.userRegister(body);
  }

  // @UseGuards(AuthGuard('jwt'))
  @Post("login")
  async login(@Body() body) {
    // JWT验证 - Step 1: 用户请求登录
    console.log("JWT验证 - Step 1: 用户请求登录");
    let authResult = await this.authService.validateUser(body);
    // let authResult = await this.localStrategy.validate(body);
    if (authResult) {
      return this.authService.login(authResult);
    } else return {
      code: 600,
      msg: "验证失败，请检查用户名和密码是否正确"
    };
  }

  // @UseGuards(AuthGuard("jwt"))
  @Get("profile")
  getProfile(@Body() body) {
    return body;
  }
}
